added SSCLI 1.0
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CSVSAddInCommandEvents / ReadMe.txt
blobd3de8f98ae7176a44eb85949f58f45dc97636060
1 ========================================================================
2     Visual Studio Add-In : CSVSAddInCommandEvents Project Overview
3 ========================================================================
5 /////////////////////////////////////////////////////////////////////////////
6 Use:
8 This sample demonstrates how to subscribe to the shell command executing and
9 how to change the menu item's caption dynamically.
11 The EnvDTE Automation has provided CommandEvents interface to represent the
12 specific command events in the shell.
13 The DTE.Events.get_CommandEvents() method gives the way to get the specific
14 command events, then you could add your personal actions before/after the
15 command executing.
17 To change the text of menu item in AddIn, you need to get the CommandBar
18 which contains the menu item controls firstly, then use
19 CommandBarControl.Caption property to specify the caption of menu item.
22 /////////////////////////////////////////////////////////////////////////////
23 Deployment:
25 1. Open CSVSAddInCommandEvents.AddIn file and change the Assembly element to
26 the path of the CSVSAddInCommandEvents.dll file.
28 2. Copy the CSVSAddInCommandEvents.AddIn file to directory:
29 %userprofile%\Documents\Visual Studio 2008\Addins\
32 /////////////////////////////////////////////////////////////////////////////
33 Creation:
35 Step1. Create Visual Studio Add-in project from File -> New -> Project
36 -> Other Project Types -> Extensibility -> Visual Studio Add-In.
38 Step2. When you create an add-in by using the Add-In Wizard and select
39 the option to display it as a command, the command is on the Tools
40 menu by default.
42 Step3. In the add-in's Connect class and OnConnection() procedure,
43 modify the command with name CSVSAddInCommandEvents and button text
44 Add CommandEvent Subscription:
46 Command command
47     = commands.AddNamedCommand2(_addInInstance,
48     "CSVSAddInCommandEvents",
49     "Add CommandEvent Subscription",
50     "Executes the command for CSVSAddInCommandEvents",
51     true, 59, ref contextGUIDS,
52     (int)vsCommandStatus.vsCommandStatusSupported
53     + (int)vsCommandStatus.vsCommandStatusEnabled,
54     (int)vsCommandStyle.vsCommandStylePictAndText,
55     vsCommandControlType.vsCommandControlTypeButton);
57 Step4. Add variables for the CommandEvents and menu item's state:
59 private CommandEvents addReferenceEvents; // Command events.
60 private bool isSubscribe; // Flag to indicate the menu item's current work.
62 Step5. Initialize the isSubscribe to true in constructor:
63 isSubscribe = true;
65 Step6. Define a method to add subscription of the Project.AddReference
66 command with below contents:
68 public void AddSubscription()
70     // "{1496A755-94DE-11D0-8C3F-00C04FC2AAE2}, 1113" Guid-ID pair refer to
71     // Project.AddReference command.
72     // About how to get the Guid and ID of the specific command, please take
73     // a look at this link on Dr.eX's blog:
74     // http://blogs.msdn.com/dr._ex/archive/2007/04/17/using-
75     // enablevsiplogging-to-identify-menus-and-commands-with-vs-2005-sp1.aspx
76     try
77     {
78         addReferenceEvents
79             = _applicationObject.Events.get_CommandEvents(
80             "{1496A755-94DE-11D0-8C3F-00C04FC2AAE2}",
81             1113);
82         addReferenceEvents.BeforeExecute
83             += new _dispCommandEvents_BeforeExecuteEventHandler
84                 (addReferenceEvents_BeforeExecute);
85         addReferenceEvents.AfterExecute
86             += new _dispCommandEvents_AfterExecuteEventHandler
87                 (addReferenceEvents_AfterExecute);
88     }
89     catch (Exception e)
90     {
91         System.Windows.Forms.MessageBox.Show(e.Message);
92     }
95 Step7. Define a method to remove subscription of the Project.AddReference
96 command with below contents:
98 public void RemoveSubscription()
100     try
101     {
102         addReferenceEvents.BeforeExecute
103             -= new _dispCommandEvents_BeforeExecuteEventHandler
104                 (addReferenceEvents_BeforeExecute);
105         addReferenceEvents.AfterExecute
106             -= new _dispCommandEvents_AfterExecuteEventHandler
107                 (addReferenceEvents_AfterExecute);
108     }
109     catch (Exception e)
110     {
111         System.Windows.Forms.MessageBox.Show(e.Message);
112     }
115 Step8. Implement the BeforeExecute/AfterExecute event handlers:
117 public void addReferenceEvents_BeforeExecute(string Guid,
118     int ID,
119     object CustomIn,
120     object CustomOut,
121     ref bool CancelDefault)
123     System.Windows.Forms.MessageBox.Show("Before adding reference.");
125     // If you want to cancel the default handler for this command, specify
126     // the CancelDefault to true.
128     //CancelDefault = true;
131 public void addReferenceEvents_AfterExecute(string Guid,
132     int ID,
133     object CustomIn,
134     object CustomOut)
136     System.Windows.Forms.MessageBox.Show("After adding reference.");
139 Step9. Modify the Exec() procedure.
140 Call the AddSubscription() or RemoveSubscription() method, change the caption
141 of menu item depends on the flag isSubscribe:
143 public void Exec(string commandName,
144     vsCommandExecOption executeOption,
145     ref object varIn,
146     ref object varOut,
147     ref bool handled)
149     handled = false;
150     if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
151     {
152         if (commandName
153             == "CSVSAddInCommandEvents.Connect.CSVSAddInCommandEvents")
154         {
155             // Get the Tools command bar.
156             Microsoft.VisualStudio.CommandBars.CommandBar menuBarCommandBar
157                 = ((Microsoft.VisualStudio.CommandBars.CommandBars)
158                 _applicationObject.CommandBars)["MenuBar"];
159             CommandBarControl toolsControl
160                 = menuBarCommandBar.Controls["Tools"];
161             CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;
162             CommandBar toolsCommandBar = toolsPopup.CommandBar;
163             if (isSubscribe)
164             {
165                 // Subscribe to the command events.
166                 AddSubscription();
167                 // Next clicking will remove the subscription.
168                 isSubscribe = false;
169                 // Get the menu item control.
170                 CommandBarControl menuItemControl
171                     = toolsCommandBar.Controls["Add Subscription"];
172                 // Change its caption to Remove Subscription.
173                 menuItemControl.Caption = "Remove CommandEvent Subscription";
174             }
175             else
176             {
177                 // Remove the subscription.
178                 RemoveSubscription();
179                 // Next clicking will add the subscription.
180                 isSubscribe = true;
181                 // Get the menu item control.
182                 CommandBarControl menuItemControl
183                     = toolsCommandBar.Controls["Remove Subscription"];
184                 // Change its caption to Add Subscription.
185                 menuItemControl.Caption = "Add CommandEvent Subscription";
186             }
188             handled = true;
189             return;
190         }
191     }
194 Step10. Compile the project.
197 /////////////////////////////////////////////////////////////////////////////
198 References:
200 Using EnableVSIPLogging to identify menus and commands with VS 2005 + SP1:
201 http://blogs.msdn.com/dr._ex/archive/2007/04/17/using-enablevsiplogging-to-
202 identify-menus-and-commands-with-vs-2005-sp1.aspx
204 CommandEvents Interface:
205 http://msdn.microsoft.com/en-us/library/envdte.commandevents.aspx
207 _DTE.Events Property:
208 http://msdn.microsoft.com/en-us/library/envdte._dte.events.aspx 
211 /////////////////////////////////////////////////////////////////////////////